﻿using IPCLibrary;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading;
using System.Text;
namespace IPCLibraryTest
{
    
    
    /// <summary>
    ///This is a test class for MailslotServerTest and is intended
    ///to contain all MailslotServerTest Unit Tests
    ///</summary>
	[TestClass()]
	public class MailslotServerTest
	{
		private int _readCounter;

		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion


		/// <summary>
		///A test for ReadAsync
		///</summary>
		[TestMethod()]
		public void ReadAsyncTest()
		{
			string mailslotName = "TestServer";
			Mailslot target = new Mailslot(mailslotName);
			target.Create();

			_readCounter = 0;
			target.ReadCompleted += new System.EventHandler<ReadCompletedEventArgs>(target_ReadCompleted);

			bool expected = true;
			bool actual;
			actual = target.ReadAsync();
			Assert.AreEqual(expected, actual);

			// Send lots of messages
			int msgCount = 500;
			MailslotClient client = new MailslotClient(mailslotName);

			for (int i = 0; i < 500; i++)
			{
				client.SendMessage("This is a test");
			}
			Thread.Sleep(2000);
			Assert.AreEqual(msgCount, _readCounter, "Did not read all messages asynchronously");
			target.Dispose();
		}

		void target_ReadCompleted(object sender, ReadCompletedEventArgs e)
		{
			_readCounter++;
			(sender as Mailslot).ReadAsync();
		}

		/// <summary>
		///A test for NextMessageSize
		///</summary>
		[TestMethod()]
		public void NextMessageSizeTest()
		{
			string mailslotName = "testslot";
			Mailslot target = new Mailslot(mailslotName);
			target.Create();

			MailslotClient client = new MailslotClient(mailslotName);
			client.SendMessage(mailslotName);

			int expected = Encoding.ASCII.GetBytes(mailslotName).Length;
			int actual;
			actual = target.NextMessageSize;

			Assert.AreEqual(expected, actual, "Next message size is not correct.");
			target.Dispose();

		}

		/// <summary>
		///A test for MessageCount
		///</summary>
		[TestMethod()]
		public void MessageCountTest()
		{
			string mailslotName = "testslot";
			Mailslot target = new Mailslot(mailslotName);
			target.Create();

			MailslotClient client = new MailslotClient(mailslotName);

			int expected = 0;
			int actual;
			actual = target.MessageCount;
			Assert.AreEqual(expected, actual, "Message Count is not correct.");

			client.SendMessage("Test");
			expected++;
			actual = target.MessageCount;
			Assert.AreEqual(expected, actual, "Message Count is not correct.");

			client.SendMessage("Test");
			expected++;
			actual = target.MessageCount;
			Assert.AreEqual(expected, actual, "Message Count is not correct.");

			target.Read();
			expected--;
			actual = target.MessageCount;
			Assert.AreEqual(expected, actual, "Message Count is not correct.");
			target.Dispose();
			client.Dispose();
		}

		/// <summary>
		///A test for MaxMessageSize
		///</summary>
		[TestMethod()]
		public void MaxMessageSizeTest()
		{
			string mailslotName = "testslot";
			Mailslot target = new Mailslot(mailslotName);
			target.Create();
			int actual;
			int expected = 424;
			actual = target.MaxMessageSize;
			Assert.AreEqual(expected, actual);
			target.Dispose();
		}

	}
}
